#include "game.h"
#include "ui_game.h"
#include <QPainter>
#include <QPixmap>
#include <QBitmap>
#include <QPen>
#include <QMouseEvent>

//当按钮按下和接受到对面的下棋消息时在widget中调用
void game::tong()
{
  if(0==pattern)
  {
    mod->setText(tr("元素大师"));
    jiepattern->setText(tr("元素大师模式\n当所有元素格子被点亮时，计算得分，同种元素点亮数量越多，分值越高"));
  }
  else if (1==pattern) {
    mod->setText(tr("四角逐鹿"));
    jiepattern->setText(tr("四角逐鹿模式\n当所有四个角落的元素被点亮时，点亮角落数量多的一方获胜"));
  }
  else if (2==pattern) {
    mod->setText(tr("全面战争"));
    jiepattern->setText(tr("全面战争模式\n当所有元素格子被点亮时，点亮格子数多的一方获胜"));
  }

}

void game::chu()
{
  state=rand()%4;
  pattern=rand()%3;
  f=1;

  zhitouzi->setEnabled(false);
  zhitouzi->show();
  chongzhitouzi->close();
  guo->close();

  if(0==pattern)
  {
    mod->setText(tr("元素大师"));
    jiepattern->setText(tr("元素大师模式\n当所有元素格子被点亮时，计算得分，同种元素点亮数量越多，分值越高"));
  }
  else if (1==pattern) {
    mod->setText(tr("四角逐鹿"));
    jiepattern->setText(tr("四角逐鹿模式\n当所有四个角落的元素被点亮时，点亮角落数量多的一方获胜"));
  }
  else if (2==pattern) {
    mod->setText(tr("全面战争"));
    jiepattern->setText(tr("全面战争模式\n当所有元素格子被点亮时，点亮格子数多的一方获胜"));
  }

  for (int i=0;i<5;i++)
  {
    QVector<Pin> temp;
    for(int j=0;j<5;j++)
    {
      if(i==2&&j==2)
      {
        temp.push_back(-1);
        continue;
      }
      qint8 sea=-1;
      while(1)
      {
        sea=rand()%4;
        if(four[sea]<=5)
        {
          four[sea]++;
          break;
        }
      }
      temp.push_back(sea);
    }
    p.push_back(temp);
  }

}

void game::zuofen()
{
  //当模式为元素大师的计分方式
  if(0==pattern)
  {
    QVector<int> a(4,0);
    QVector<int> b(4,0);
    for (int i=0;i<5;i++)
    {
      for(int j=0;j<5;j++)
      {
        if(p[i][j].pin_state==1)
        {
          a[p[i][j].pin_season]++;
        }
        if(p[i][j].pin_state==2)
        {
          b[p[i][j].pin_season]++;
        }
      }
    }
    for(int i=0;i<4;i++)
    {
      if(fen1<a[i])
      {
        fen1=a[i];
      }
    }
    for(int i=0;i<4;i++)
    {
      if(fen2<b[i])
      {
        fen2=b[i];
      }
    }
  }

  //当模式为四角逐鹿的计分方式
  if(1==pattern)
  {
    int a=0,b=0;
    if(1==p[0][0].pin_state)
    {
      a++;
    }
    else if(2==p[0][0].pin_state)
    {
      b++;
    }

    if(1==p[4][4].pin_state)
    {
      a++;
    }
    else if(2==p[4][4].pin_state)
    {
      b++;
    }

    if(1==p[0][4].pin_state)
    {
      a++;
    }
    else if(2==p[0][4].pin_state)
    {
      b++;
    }

    if(1==p[4][0].pin_state)
    {
      a++;
    }
    else if(2==p[4][0].pin_state)
    {
      b++;
    }
    fen1=a,fen2=b;
  }

  //当模式为全面战争的计分方式
  if(2==pattern)
  {
    int a=0,b=0;
    for(int i=0;i<5;i++)
    {
      for(int j=0;j<5;j++)
      {
        if(1==p[i][j].pin_state)
        {
          a++;
        }
        else if(2==p[i][j].pin_state)
        {
          b++;
        }
      }
    }
    fen1=a,fen2=b;
  }

  QString str1=QString("%1").arg(fen1);
  QString str2=QString("%1").arg(fen2);
}

game::game(QWidget *parent) :
QWidget(parent),
ui(new Ui::game)
{
  ui->setupUi(this);
  setMouseTracking(true);
  srand(time(NULL));
  setWindowTitle(tr("元素棋"));
  setFixedSize(1110,530);
  QFont ft;
  ft.setPointSize(15);
  score->setFont(QFont("黑体",15));
  QPalette pa;
  pa.setColor(QPalette::WindowText,QColor(208,175,195));
  score->setPalette(pa);
  score->setText(tr("记分牌"));
  score->move(94,60);

  pa.setColor(QPalette::WindowText,QColor(78,198,250));
  score1->setPalette(pa);
  score1->setFont(QFont("黑体",15));
  QString s1=QString("%1").arg(fen1);
  score1->setText(s1);
  score1->move(62,82);

  pa.setColor(QPalette::WindowText,QColor(177,240,198));
  score2->setPalette(pa);
  score2->setFont(QFont("黑体",15));
  QString s2=QString("%1").arg(fen2);
  score2->setText(s2);
  score2->move(195,82);


  state=rand()%4;
  pattern=rand()%3;
  jiestate->resize(246,79);
  jiestate->setText(tr("四季流转，在当前季节掷出对应的元素时，则该次的元素点亮没有范围限制，可以点亮不相邻的元素"));
  jiestate->setWordWrap(true);
  jiepattern->resize(363,91);
  jiepattern->setWordWrap(true);

  for(int i=0;i<5;i++)
  {
    for(int j=0;j<5;j++)
    {
      buton[i][j].setMinimumSize(96,96);
      buton[i][j].setMaximumSize(96,96);
      buton[i][j].move(304+j*100,10+i*100);
      buton[i][j].setParent(this);
      buton[i][j].setFlat(true);
      buton[i][j].setEnabled(false);
    }
  }

  //初始化棋盘内容
  for (int i=0;i<5;i++)
  {
    QVector<Pin> temp;
    for(int j=0;j<5;j++)
    {
      if(i==2&&j==2)
      {
        temp.push_back(-1);
        continue;
      }
      qint8 sea=-1;
      while(1)
      {
        sea=rand()%4;
        if(four[sea]<=5)
        {
          four[sea]++;
          break;
        }
      }
      temp.push_back(sea);
    }
    p.push_back(temp);
  }
  update();

  //游戏模式显示
  mode->setText(tr("当前模式"));
  mode->setFont(QFont("华文琥珀",18));
  mode->move(18,20);
  pa.setColor(QPalette::WindowText,QColor(200,191,231));
  mode->setPalette(pa);

  mod->setFont(QFont("宋体",18,75));
  mod->move(143,20);
  pa.setColor(QPalette::WindowText,QColor(255,236,147));
  mod->setPalette(pa);
  if(0==pattern)
  {
    mod->setText(tr("元素大师"));
    jiepattern->setText(tr("元素大师模式\n当所有元素格子被点亮时，计算得分，同种元素点亮数量越多，分值越高"));
  }
  else if (1==pattern) {
    mod->setText(tr("四角逐鹿"));
    jiepattern->setText(tr("四角逐鹿模式\n当所有四个角落的元素被点亮时，点亮角落数量多的一方获胜"));
  }
  else if (2==pattern) {
    mod->setText(tr("全面战争"));
    jiepattern->setText(tr("全面战争模式\n当所有元素格子被点亮时，点亮格子数多的一方获胜"));
  }
  //游戏模式解析窗口
  QPixmap pixpattern("../image/问号.png");
  anpattern->resize(pixpattern.size());
  anpattern->setIcon(pixpattern);
  anpattern->setIconSize(pixpattern.size());
  anpattern->setMask(pixpattern.mask());
  anpattern->move(250,-10);

  connect(anpattern,&QPushButton::clicked,[=]{
    jiepattern->move(geometry().x()+275,geometry().y()+20);
    jiepattern->show();
  });

  jiepattern->close();
  jiepattern->setWindowFlag(Qt::FramelessWindowHint);

  //季节以及游戏规则解析窗口
  QPixmap pixstate("../image/问号.png");
  anstate->resize(pixstate.size());
  anstate->setIcon(pixstate);
  anstate->setIconSize(pixstate.size());
  anstate->setMask(pixstate.mask());
  anstate->move(-10,130);

  connect(anstate,&QPushButton::clicked,[=]{
    jiestate->move(geometry().x()+20,geometry().y()+150);
    jiestate->show();
  });

  jiestate->close();
  jiestate->setWindowFlag(Qt::FramelessWindowHint);


  //右边掷骰子按钮
  QPixmap pixzhitouzi("../image/掷骰子按钮.png");
  zhitouzi->resize(pixzhitouzi.size());
  zhitouzi->setIcon(pixzhitouzi);
  zhitouzi->setIconSize(pixzhitouzi.size());
  zhitouzi->setMask(pixzhitouzi.mask());
  zhitouzi->move(842,190);
  zhitouzi->setEnabled(false);
  connect(zhitouzi,&QPushButton::clicked,[=]{
    sezi1=rand()%4;
    this->update();
    zhitouzi->close();
    chongzhitouzi->show();
    chongzhitouzi->setEnabled(true);
    guo->show();
    if(1==f||state==sezi1)
    {
      f=0;
      for(int i=0;i<5;i++)
      {
        for(int j=0;j<5;j++)
        {
          if(p[i][j].pin_season==sezi1
              &&p[i][j].pin_state==Pin::unmanned)
          //己方第一次落子，与骰子元素相同且是无人区则打开按钮
          {
            buton[i][j].setEnabled(true);
          }
        }
      }
    }
    else
    {
      for(int i=0;i<5;i++)
      {
        for(int j=0;j<5;j++)
        {
          if(p[i][j].pin_season==sezi1
              &&p[i][j].pin_state==Pin::unmanned)
          //上下左右是己方且与骰子元素相同且是无人区，打开按钮
          {
            if(i-1>0&&p[i-1][j].pin_state==1)
            {
              buton[i][j].setEnabled(true);
            }
            if(i+1<5&&p[i+1][j].pin_state==1)
            {
              buton[i][j].setEnabled(true);
            }
            if(j-1>0&&p[i][j-1].pin_state==1)
            {
              buton[i][j].setEnabled(true);
            }
            if(j+1<5&&p[i][j+1].pin_state==1)
            {
              buton[i][j].setEnabled(true);
            }
          }
        }
      }

    }
  });


  //右边重置骰子
  chongzhitouzi->close();
  QPixmap pixchongzhitouzi("../image/重置骰子按钮.png");
  chongzhitouzi->resize(pixchongzhitouzi.size());
  chongzhitouzi->setIcon(pixchongzhitouzi);
  chongzhitouzi->setIconSize(pixchongzhitouzi.size());
  chongzhitouzi->setMask(pixchongzhitouzi.mask());
  chongzhitouzi->move(845,190);
  connect(chongzhitouzi,&QPushButton::clicked,[=]{
    sezi1=rand()%4;
    this->update();
    chongzhitouzi->setEnabled(false);
    if(1==f||state==sezi1)
    {
      for(int i=0;i<5;i++)
      {
        for(int j=0;j<5;j++)
        {
          if(p[i][j].pin_season==sezi1)
          {
            buton[i][j].setEnabled(true);
          }
          else
          {
            buton[i][j].setEnabled(false);
          }
        }
      }
    }
    else
    {
      for(int i=0;i<5;i++)
      {
        for(int j=0;j<5;j++)
        {
          if(p[i][j].pin_season==sezi1
              &&p[i][j].pin_state==Pin::unmanned)
          //上下左右是己方且与骰子元素相同且是无人区，打开按钮
          {
            if(i-1>0&&p[i-1][j].pin_state==1)
            {
              buton[i][j].setEnabled(true);
            }
            if(i+1<5&&p[i+1][j].pin_state==1)
            {
              buton[i][j].setEnabled(true);
            }
            if(j-1>0&&p[i][j-1].pin_state==1)
            {
              buton[i][j].setEnabled(true);
            }
            if(j+1<5&&p[i][j+1].pin_state==1)
            {
              buton[i][j].setEnabled(true);
            }
          }
        }
      }

    }
  });


  //右边跳过回合
  guo->close();
  QPixmap pixguo("../image/跳过回合按钮.png");
  guo->resize(pixguo.size());
  guo->setIcon(pixguo);
  guo->setIconSize(pixguo.size());
  guo->setMask(pixguo.mask());
  guo->move(965,190);
  //过的消息在主窗口写，并发送跳过消息

  //右边认输按钮
  QPixmap pixrenshu("../image/认输按钮.png");
  renshu->resize(pixrenshu.size());
  renshu->setIcon(pixrenshu);
  renshu->setIconSize(pixrenshu.size());
  renshu->setMask(pixrenshu.mask());
  renshu->move(820,460);
  //认输的消息在主窗口写，并发送认输消息


}

game::~game()
{
  delete ui;
}

void game::paintEvent(QPaintEvent *)
{

  QString s1=QString("%1").arg(fen1);
  score1->setText(s1);

  QString s2=QString("%1").arg(fen2);
  score2->setText(s2);

  QPainter *pain=new QPainter(this);
  QBrush brush(QColor(56,32,75));
  pain->setBrush(brush);
  pain->drawRect(0,0,1110,530);
  brush=(QColor(71,42,94));
  pain->setBrush(brush);
  QPointF point[3]={
      QPoint(0,530),
      QPoint(555,0),
      QPoint(1110,530)};
  pain->drawPolygon(point,3);

  QPen pen(QColor(255,236,147),2);
  pain->setPen(pen);
  pain->drawRoundRect(0,150,280,380,10,10);

  //右边的游戏进行
  pain->drawRoundRect(810,10,290,510,5,5);
  pain->drawPixmap(964,29,125,114,QPixmap("../image/毛毛比"));
  pain->drawPixmap(964,380,125,114,QPixmap("../image/毛毛理"));


  //当前季节
  if (state==0) {
    pain->drawPixmap(27,183,227,314,QPixmap("../image/春风.png"));
  }
  else if (state==1) {
    pain->drawPixmap(27,183,227,314,QPixmap("../image/夏火.png"));
  }
  else if (state==2) {
    pain->drawPixmap(27,183,227,314,QPixmap("../image/秋水.png"));
  }
  else if (state==3) {
    pain->drawPixmap(27,183,227,314,QPixmap("../image/冬土.png"));
  }

  //记分牌和游戏模式的载体
  pen.setColor(QColor(56,34,72));
  pain->setPen(pen);
  brush.setColor(QColor(91,58,115));
  pain->drawRect(1,1,278,140);

  //游戏模式，文字通过label设置，这里只画图
  brush.setColor(QColor(56,34,72));

  pain->setBrush(brush);
  pain->drawRect(139,14,127,43);


  //记分牌
  pen.setColor(QColor(105,71,131));
  pain->setPen(pen);
  pain->drawRoundRect(12,72,253,46,10,10);
  pain->drawLine(133,72,133,118);

  //右边两骰子
  QString wanjia1=QString("../image/骰子%1").arg(sezi1);
  QString wanjia2=QString("../image/骰子%1").arg(sezi2);
  pain->drawPixmap(815,350,100,100,QPixmap(wanjia1));
  pain->drawPixmap(815,29,100,100,QPixmap(wanjia2));




  //给按钮贴背景图
  for(int i=0;i<5;i++)
  {
    for(int j=0;j<5;j++)
    {
      int s=p[i][j].pin_state;
      QString ss;
      if(p[i][j].pin_season==0)
      {
        ss="春风";
      }
      else if (p[i][j].pin_season==1)
      {
        ss="夏火";
      }
      else if (p[i][j].pin_season==2)
      {
        ss="秋水";
      }
      else if (p[i][j].pin_season==3)
      {
        ss="冬土";
      }
      else if (p[i][j].pin_season==4)
      {
        ss="标志";
      }
      QString pix=QString("../image/%1%2.png").arg(ss).arg(s);
      pain->drawPixmap(304+j*100,10+i*100,96,96,QPixmap(pix));
    }
  }





}

void game::mousePressEvent(QMouseEvent *e)
{

}

void game::mouseMoveEvent(QMouseEvent *e)
{
  jiepattern->close();

  jiestate->close();
}

void game::mouseReleaseEvent(QMouseEvent *e)
{

}








